package me.project.jobster.service;

import me.project.jobster.common.constant.GlobalConstants;
import me.project.jobster.common.util.DateUtils;
import me.project.jobster.dao.mybatis.mapper.FollowMapper;
import me.project.jobster.dao.mybatis.mapper.NotificationMapper;
import me.project.jobster.dao.mybatis.model.Follow;
import me.project.jobster.dao.mybatis.model.Notification;
import me.project.jobster.dao.mybatis.model.Student;
import me.project.jobster.pojo.Applications;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Mcdull
 * @date 2018-4-27
 */
@Service
public class MessageService {

    @Autowired
    private NotificationMapper notificationMapper;
    @Autowired
    private FollowMapper followMapper;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * get all the receiver meaasge
     * @param userId
     * @return
     */
    public List<Notification> getAllMessage(String userId, String status){
        Notification notification = new Notification();
        notification.setReceiver(userId);
        notification.setStatus(status);
        return notificationMapper.select(notification);
    }

    /**
     * mark the message as read
     * @param id
     */
    public void markAsRead(Integer id) {
        Notification notification = new Notification();
        notification.setId(id);
        notification.setStatus(GlobalConstants.MSG_READ);
        notificationMapper.updateByPrimaryKeySelective(notification);
    }

    /**
     * send a message
     * @param notification
     */
    public void sendMessage(Notification notification){
        notification.setTime(DateUtils.formatNow("yyyy-MM-dd HH:mm:ss:SSS"));
        notificationMapper.insert(notification);

    }

    /**
     * group message
     * @param companyId
     * @param companyName
     * @param positionId
     */
    public void groupMessage(String companyId, String companyName,String positionId){
        Follow follow = new Follow();
        follow.setcId(companyId);
        List<Follow> list = followMapper.select(follow);
        for (Follow f : list) {
            Notification notification = new Notification();
            notification.setType(GlobalConstants.MSG_GROUP_SENDING);
            notification.setStatus(GlobalConstants.MSG_UNREAD);
            notification.setReceiver(f.getsId());
            notification.setSender(companyId);
            notification.setSendName(companyName);
            notification.setExt1(positionId);
            notification.setMassage(companyName + " has post a job just now!");
            notification.setTime(DateUtils.formatNow("yyyy-MM-dd HH:mm:ss:SSS"));
            notificationMapper.insert(notification);
        }
    }

    /**
     * get all application
     * @param userId
     * @return
     */
    public List getApplications(String userId){
        String sql = "select b.p_id pId, b.title,a.sender, a.send_name sendName from notification a left join position b on a.ext1 = b.p_id where a.type = '1' and a.receiver = '"+userId+"' order by b.p_id desc ";

        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(Applications.class));
    }
}
